#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include "axil_cmd.h"
#include <cstring>


int axil_cmd_setup(axil_cmd_handle *handle, unsigned int baseAddr) {
    handle->baseAddr=baseAddr;
    handle->axil_cmd_handler = open("/dev/mem", O_RDWR | O_SYNC);
    handle->axil_cmd_VirtualAddress = (unsigned int*)mmap(NULL, 65535, PROT_READ | PROT_WRITE, MAP_SHARED, handle->axil_cmd_handler, (off_t)handle->baseAddr);
    if(handle->axil_cmd_VirtualAddress == MAP_FAILED) {
        perror("gpioVirtualAddress mapping for absolute memory access failed.\n");
        return -1;
    }
    else
        return 0;
}

void axil_cmd_set(axil_cmd_handle *handle, int num, unsigned int val){
    handle->axil_cmd_VirtualAddress[num>>2]=val;
}

unsigned int axil_cmd_get(axil_cmd_handle *handle, int num){
    return handle->axil_cmd_VirtualAddress[num>>2];
}
